package com.jintoufs.service.ad.impl;

import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.ad.AdMapper;
import com.jintoufs.domain.ad.Ad;
import com.jintoufs.service.ad.AdService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xufei on 2017/12/20.
 */
@Service
public class AdServiceImpl implements AdService {
    @Resource
    private AdMapper adMapper;

    @Override
    public List<Map<String,Object>> queryByParams(Map<String, Object> params) {
        return adMapper.queryByParams(params);
    }

    @Override
    public Map<String, Object> save(Ad ad) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (ad.getId() == null || "".equals(ad.getId())) {
                adMapper.insertSelective(ad);
                result.put("msg", "新增成功");
            } else {
                adMapper.updateByPrimaryKeySelective(ad);
                result.put("msg", "更新成功");
            }
            result.put("status", "OK");
            return result;
        } catch (Exception e) {
            result.put("msg", "操作失败");
            result.put("status", "ERROR");
            return result;
        }
    }

    @Override
    public void delete(Integer id) {
        adMapper.deleteByPrimaryKey(id.toString());
    }

    @Override
    public List<Ad> getByFilter(QueryFilters filters) {
        List<Ad> result = adMapper.getByFilter(filters);
        return result;
    }

    @Override
    public Ad getById(Integer id) {
        return adMapper.queryByPrimaryKey(id);
    }

    @Override
    public Ad getLastedAd() {
        return adMapper.getLastedAd();
    }
}
